﻿using PersonalDiary.Album.Control;
using PersonalDiary.Album.Model;
using PersonalDiary.Album.View;
using PersonalDiary.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace PersonalDiary.Album.ViewModel
{
    public class AlbumImageViewModel
    {

        #region Property
        private ObservableCollection<ImageSourceModel> _imageList;
        public ObservableCollection<ImageSourceModel> ImageList
        {
            get
            {
                if (_imageList == null)
                {
                    _imageList = new ObservableCollection<ImageSourceModel>();
                }
                return _imageList;
            }
        }

        private string _SelectItemFilePath;
        public string SelectItemFilePath
        {
            get
            {
                return _SelectItemFilePath;
            }
            set
            {
                _SelectItemFilePath = value;
            }
        }

        private ContextMenu _ContextMenuItem;
        public ContextMenu ContextMenuItem
        {
            get
            {
                if (_ContextMenuItem == null)
                {
                    _ContextMenuItem = new ContextMenu();
                    _ContextMenuItem.Visibility = Visibility.Collapsed;
                }

                return _ContextMenuItem;
            }
            set
            {
                _ContextMenuItem = value;
                _ContextMenuItem.Visibility = Visibility.Visible;
            }
        }

        //---
        //private bool _ImageTransFormVisibilityCheck = false;
        //public bool ImageTransFormVisibilityCheck
        //{
        //    get
        //    {
        //        return _ImageTransFormVisibilityCheck;
        //    }
        //    set
        //    {
        //        _ImageTransFormVisibilityCheck = value;
        //    }
        //}
        
        //private Visibility _ImageTransFormVisibilityCheck = Visibility.Hidden;
        //public Visibility ImageTransFormVisibilityCheck
        //{
        //    get
        //    {
        //        return _ImageTransFormVisibilityCheck;
        //    }
        //    set
        //    {
        //        _ImageTransFormVisibilityCheck = value;
        //    }
        //}

        #endregion

        #region Command
        private RelayCommand _selectFolderCommand;
        public ICommand SelectFolderCommand
        {
            get
            {
                return _selectFolderCommand = _selectFolderCommand ?? new RelayCommand((s) => OnSelectFolderCommandExecuted());
            }
        }

        private void OnSelectFolderCommandExecuted()
        {
            OpenFileDialogForSelectingFolder();
        }

        private void OpenFileDialogForSelectingFolder()
        {
            var SelectImagesPath = OpenFileMethod();

            if (SelectImagesPath != null)
            {

                foreach (var item in SelectImagesPath)
                {
                    var fileinfo = new FileInfo(item);
                    var ImgModel = new ImageSourceModel() { FilePath = item, FileName = fileinfo.Name, ImageBitmapSource = CreateBitmapImage(item) };
                    
                    ImageList.Add(ImgModel);
                }
            }
        }

        //TransForm 커맨드
        private RelayCommand _ImageTransFormCommand;
        public ICommand ImageTransFormCommand
        {
            get
            {
                return _ImageTransFormCommand = _ImageTransFormCommand ?? new RelayCommand((s) => ImageTransFormCommandExecuted());
            }
        }

        private void ImageTransFormCommandExecuted()
        {
            ObservableCollection<ImageSourceModel> SaveImageList = null;

            ImageTransFormSupportView SupportView = new ImageTransFormSupportView();
            
            //선택된 이미지만 담기도록 만들어야 하고
            SaveImageList = this.ImageList;

            foreach (var item in SaveImageList)
            {
                BitmapSource TemporaryImage = CreateBitmapImage(item.FilePath);
                AlbumImageTransForm AITF = new AlbumImageTransForm(TemporaryImage);

                SupportView.MainGrid.Children.Add(AITF);
            }

            
            //--------- 화면 팝업창 형식으로 띄어야 하고 ... 소스 들어가야 하고 
            SupportView.MainGrid.Width = 800;
            SupportView.MainGrid.Height = 600;

            //SupportView.Visibility = Visibility.Visible;
        }

        private RelayCommand _ContextMenuCommand;
        public ICommand ContextMenuCommand
        {
            get
            {
                return _ContextMenuCommand = _ContextMenuCommand ?? new RelayCommand((s) => OnContextMenuCommandExecuted());
            }
        }

        private void OnContextMenuCommandExecuted()
        {
            MenuItem TransFrom = new MenuItem();
            TransFrom.Header = "ImageCollage";
            TransFrom.Command = null;


        }
        #endregion

        #region Method
        private string[] OpenFileMethod()
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.InitialDirectory = "C:\\Users\\Public\\Pictures";
            openFileDialog.Filter = "Image Files(*.BMP;*.JPG;*.PNG)|*.BMP;*.JPG;*.PNG";
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == true)
            {
                return openFileDialog.FileNames;
            }
            return null;
        }

        private BitmapSource CreateBitmapImage(string SelectImagesPath)
        {
            BitmapImage bitmapImg = new BitmapImage();
            bitmapImg.BeginInit(); //이미지 초기화
            bitmapImg.CacheOption = BitmapCacheOption.OnLoad; //비트맵 이미지 설정 방식 지정
            bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;  //비트맵 이미지 설정 방식 지정

            bitmapImg.UriSource = new Uri(SelectImagesPath, UriKind.Absolute);
            bitmapImg.EndInit();

            return bitmapImg;
        }
        #endregion
    }
}
